package com.njlime.member.service.app;

import com.njlime.cms.entities.PublishReport;
import com.njlime.core.utils.DateUtil;
import com.njlime.member.repository.app.AppPublishReportDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class AppPublishReportService {

    @Resource
    private AppPublishReportDao appPublishReportDao;

    @Transactional
    public void save(Integer business_type,Integer business_id, Integer unit_id,Integer business_unit){
        if(unit_id == null){
            unit_id = 0;
        }
        Map<String,Object> report = findReport(DateUtil.dateFormat(new Date(),"yyyy-MM-dd"),business_id,unit_id,business_type);
        if(report != null && report.get("id") != null){
            appPublishReportDao.updateReading(Integer.valueOf(report.get("id").toString()));
        }else{
            PublishReport publishReport = new PublishReport();
            publishReport.setBusiness_id(business_id);
            publishReport.setBusiness_type(business_type);
            publishReport.setBusiness_unit(business_unit);
            publishReport.setUnit_id(unit_id);
            publishReport.setDate(new Date());
            publishReport.setReading(1);
            appPublishReportDao.save(publishReport);
        }

    }

    public Map<String,Object> findReport(String date, Integer id, Integer unit_id, Integer type){
        return appPublishReportDao.findReport(date,id,unit_id,type);
    }
}
